/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.server.application;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.Collection;
import org.toools.gwt.wmc.client.application.ApplicationService;
import org.toools.gwt.wmc.server.dao.IApplicationDAO;
import org.toools.gwt.wmc.jpa.Application;
import org.toools.gwt.wmc.server.WMCSession;

/**
 *
 * @author manuel
 */
public class ApplicationServiceImpl extends RemoteServiceServlet implements ApplicationService {

    private IApplicationDAO applicationdao;

    /** Account DAO **/
    public void setApplicationdao( IApplicationDAO dao ) {
        this.applicationdao = dao;
    }
    public IApplicationDAO getApplicationdao() {
		return applicationdao;
	}


    public Application getApplicationByID(Integer ID) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || applicationdao.belongsTo( ID, WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return applicationdao.getApplicationByID( ID );

        return null;
    }

    public Collection<Application> getApplications() {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) )
            return applicationdao.getApplications();

        return applicationdao.getApplications( WMCSession.getAccountID( getThreadLocalRequest() ) );
    }

    public Application create(Application a) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) )
            return applicationdao.create( a );

        //check if belongs to account
        else
            return applicationdao.create( a );
    }

    public Boolean update(Application a) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || applicationdao.belongsTo( a.getId(), WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return applicationdao.update( a );

        return false;
    }

    public Boolean delete(Application a) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || applicationdao.belongsTo( a.getId(), WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return applicationdao.delete( a );

        return false;
    }
}
